<?php

require_once(WSB_MODULES_PATH . 'sherpa/models/BasketGateway.php');
require_once(WSB_MODULES_PATH . 'sherpa/forms/ApplyCoupon.php');

class Sherpa_CartController extends WSB_Xtension_Extension
{
	/**
	 * @var Zend_Session_Namespace
	 */
	private $_session;

	/**
	 * @return string
	 */
	public function renderForBackend()
	{
		return get_class($this);
	}

	/**
	 * Initialisierung
	 */
	public function init()
	{
		$this->_session = new Zend_Session_Namespace('Sherpa_Checkout');
		$this->initTranslate();
	}

	/**
	 * Index Action
	 *
	 * @param Sherpa_Form_ApplyCoupon $form
	 */
	public function indexAction(Sherpa_Form_ApplyCoupon $form = null)
	{
		$products = $this->_getBasketProducts();
		if(is_null($form)) {
			$form = $this->_getForm($products);
		}
		// Zuletzt besuchtes Produkt in Session speichern
		$session = new Zend_Session_Namespace('Sherpa_ProductDetails');
		$lastVisitedProductUrl = empty($session->lastVisitedProductUrl) ? '' : $session->lastVisitedProductUrl;
		$this->assignToView('lastVisitedProductUrl', $lastVisitedProductUrl);
		$this->assignToView('products', $products);
		$this->assignToView('coupon', $this->_getCoupon());
		$this->assignToView('form', $form);
		$this->renderView('cart/index.phtml');
	}

	/**
	 * Apply Coupon Code Action
	 */
	public function applyCouponCodeAction()
	{
		$request = $this->getRequest();
		$products = $this->_getBasketProducts();
		$form = $this->_getForm($products);
		if($request->isPost() && $form->isValid($request->getPost())) {
			$this->_session->couponCode = $form->getValue('couponCode');
		}
		$this->indexAction($form);
	}

	/**
	 * Update Products Action
	 */
	public function updateProductsAction()
	{
		$productToQuantity = $this->getParam('productToQuantity', array());
		if(is_array($productToQuantity)) {
			$gateway = new Sherpa_Model_BasketGateway();
			foreach($productToQuantity AS $productId=>$quantity) {
				$gateway->updateProductQuantity($productId, $quantity);
			}
		}
		$this->redirect($this->_view->urlThisPage());
	}

	/**
	 * Remove Products Action
	 */
	public function removeProductAction()
	{
		$productId = intval($this->getParam('productId', 0));
		$gateway = new Sherpa_Model_BasketGateway();
		$gateway->removeProduct($productId);
		$this->redirect($this->_view->urlThisPage());
	}

	/**
	 * Remove All Action
	 */
	public function removeAllAction()
	{
		$gateway = new Sherpa_Model_BasketGateway();
		$gateway->removeAllProducts();
		$this->redirect($this->_view->urlThisPage());
	}

	/**
	 * @return Sherpa_Model_BasketProducts
	 */
	private function _getBasketProducts()
	{
		$languageId = $this->getLanguageId();
		$gateway = new Sherpa_Model_BasketGateway();
		$products = $gateway->fetchProducts($languageId);
		$products->loadColorApplications();
		$products->loadMainImages($languageId);
		$products->loadImageVariants();
		return $products;
	}

	/**
	 * @param
	 */
	private function _getCoupon()
	{
		$couponCode = trim($this->_session->couponCode);
		$gateway = new Sherpa_Model_CouponGateway();
		return $gateway->fetchCoupon($couponCode);
	}

	/**
	 * @return Sherpa_Form_ApplyCoupon
	 */
	private function _getForm(Sherpa_Model_BasketProducts $products)
	{
		$form = new Sherpa_Form_ApplyCoupon();
		$form->setDefaults(array('couponCode' => $this->_session->couponCode));
		$form->setView($this->getView());
		$form->addSubtotalHiddenElement($products->calculateSubtotal());
		return $form;
	}

}
